package org.apache.lucene.analysis.compound.morphology;

import org.apache.lucene.analysis.util.CharArraySet;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class dePluralizer {

	public boolean isInDict(CharArraySet dict, String word) {
		char [] warr = word.toCharArray();
		return dict.contains(warr, 0, word.length());
	}
	
	public String dePluralizeDevanagari(String word, Map<String, String> words){
		for(int i = 0; i < word.length(); i++){
			String w = "";
			if(word.substring(i).equals("ाः")){
				w = word.substring(0, i) + "ः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end if
			else if(word.substring(i).equals("ा") && i == word.length()-1){
				w = word.substring(0, i) + "ः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end if
			else if(word.substring(i).equals("ानि")){
				w = word.substring(0, i) + "म्";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
			else if(word.substring(i).equals("वः")){
				w = word.substring(0, i) + "ुः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
		}//end for
		return null;
	}//end method
	
	public String dePluralize(char[] wordArr, int len, CharArraySet dict){
		String word="";
		for (int i = 0; i < len; i++)
			word += wordArr[i];
		word = word.toLowerCase();
		for(int i = 0; i < word.length() + 1; i++){
			String w = "";
			if(word.substring(i).equals("aah")){
				w = word.substring(0, i + 1) + "h";
				String w2 = word.substring(0, i+2);
				if(isInDict(dict, w)){
					return w;
				}//end if
				else if(isInDict(dict, w2)){
					return w2;
				}
			}//end if
			else if(word.substring(i).equals("aani")){
				w = word.substring(0, i+1) + "m";
				if(isInDict(dict, w)){
					return w;
				}//end if
			}//end else if
			else if(word.substring(i).equals("avah")){
				w = word.substring(0, i) + "uh";
				if(isInDict(dict, w)){
					return w;
				}//end if
			}//end else if
		}//end for
		 //return "Word is already singular.";
		return null;
	}//end method
	

}//end class
